package com.sl.core.domain.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

@Getter
@Setter
public class Transformation {
    private TransformationInfo info;


    private String notepads;


    private TransformationOrder order;


    private List<TransformationStep> step;


    @JsonProperty("step_error_handling")
    private TransformationStepErrorHandling stepErrorHandling;

    @JsonProperty("slave-step-copy-partition-distribution")
    private String slaveStepCopyPartitionDistribution;

    @JsonProperty("slave_transformation")
    private String slaveTransformation;


    public static Transformation basic(String name, String description) {
        return basic(name, description, "Normal");
    }

    public static Transformation basic(String name, String description, String transType) {
        Transformation transformation = new Transformation();
        TransformationInfo basic = TransformationInfo.basic(name, description, transType);
        transformation.setInfo(basic);
        transformation.setNotepads("");
        transformation.setStepErrorHandling(TransformationStepErrorHandling.basic());
        transformation.setSlaveStepCopyPartitionDistribution("");
        transformation.setSlaveTransformation("N");
        return transformation;
    }

    public void addHop(String from, String to) {
        this.addHop(from, to, "Y");
    }


    public void addHop(String from, String to, String enabled) {
        if (null == this.order) {
            this.order = new TransformationOrder();
        }
        this.order.addHop(from, to, enabled);
    }

    public void addStep(String name, String type, Map<String, Object> extParamMap) {
        this.addStep(name, type, "N", extParamMap);
    }

    public void addStep(String name, String type, String dynamicInputFlag, Map<String, Object> extParamMap) {
        if (null == step) {
            this.step = new ArrayList<>();
        }
        this.step.add(TransformationStep.basic(name, type, dynamicInputFlag, extParamMap));
    }

    public void rebuild() {

        if (null != this.step) {
            for (TransformationStep transformationStep : this.step) {
                transformationStep.rebuild();
                transformationStep.getGui().rebuild();
            }
        }
        if (null != this.order && null != this.order.getHop()) {
            for (TransformationOrderHop orderHop : this.order.getHop()) {
                if (null == orderHop.get("id")) {
                    orderHop.put("id", UUID.randomUUID().toString().replace("-", ""));
                }
                orderHop.rebuild();

            }
        }
    }
}
